package com.android.lovingpet.signup.view;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.base.TakePhotoFragment;
import com.android.lovingpet.common.PopupPics;
import com.android.lovingpet.signin.view.SignInActivity;
import com.android.lovingpet.signup.ILoginContract;
import com.android.lovingpet.signup.LoginInPresenter;
import com.bumptech.glide.Glide;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class LoginInTeamFragment extends TakePhotoFragment<ILoginContract.View, LoginInPresenter> implements ILoginContract.View, View.OnClickListener, PopupPics.OnPickListener, DatePickerDialog.OnDateSetListener {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.tv_eTime)
    TextView tvTime;
    @BindView(R.id.et_nature)
    EditText etNature;
    @BindView(R.id.et_hName)
    EditText etHName;
    @BindView(R.id.et_hPhone)
    EditText etHPhone;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_oPhone)
    EditText etOPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    private DatePickerDialog datePickerDialog;
    private PopupPics popupPics;
    private Unbinder unbinder;
    private File image;

    @Override
    protected int setContentView() {
        return R.layout.fragment_login_in_team;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        Calendar c = Calendar.getInstance();
        datePickerDialog = new DatePickerDialog(getContext(), this, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        popupPics = new PopupPics(getActivity(), this);
        ivHead.setOnClickListener(this);
        tvTime.setOnClickListener(this);
        ((TextView) view.findViewById(R.id.tv_title)).setText("完善组织信息");
        view.findViewById(R.id.bt_commit).setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public LoginInPresenter createPresenter() {
        return ((LoginInActivity) getActivity()).getPresenter();
    }

    @Override
    public ILoginContract.View createView() {
        return this;
    }

    @Override
    public void next() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "注册成功", Toast.LENGTH_LONG).show();
            }
        });
        startActivity(new Intent(getContext(), SignInActivity.class));
        getActivity().finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_head:
                popupPics.show(v);
                break;
            case R.id.tv_eTime:
                datePickerDialog.show();
                break;
            case R.id.bt_commit:
                getPresenter().commitInfo(image, etName.getText().toString(),
                        etAddress.getText().toString(), tvTime.getText().toString(),
                        etNature.getText().toString(), etHName.getText().toString(),
                        etHPhone.getText().toString(), etEmail.getText().toString(),
                        etOPhone.getText().toString(), etCode.getText().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(getImageCropUri(), cropOptions);
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromCaptureWithCrop(getImageCropUri(), cropOptions);
    }

    @Override
    public void takeSuccess(TResult result) {
        image = new File(result.getImage().getOriginalPath());
        Glide.with(this).load(image).into(ivHead);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        tvTime.setTextColor(Color.BLACK);
        StringBuilder date = new StringBuilder().append(year).append('-');
        if (month < 10)
            date.append('0');
        date.append(month).append('-');
        if (dayOfMonth < 10)
            date.append('0');
        date.append(dayOfMonth);
        tvTime.setText(date);
    }
}
